const express = require("express");
const app = express();
const mysql = require('mysql');
const port = 3065;
const cors = require("cors");
const multiparty = require("multiparty");
const querystring = require("querystring");
const connection = mysql.createConnection({
    host: "localhost",
    user: "root",
    password: "123456",
    database: "vue_test",
    port: 3306
});
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: false }));


// app.all("*", (req, res, next) => {
//     //设置允许跨域的域名，*代表允许任意域名跨域
//     res.header("Access-Control-Allow-Origin", "*");
//     //允许的header类型
//     res.header("Access-Control-Allow-Headers", "content-type");
//     //跨域允许的请求方式
//     res.header("Access-Control-Allow-Methods", "DELETE,PUT,POST,GET,OPTIONS");
//     if (req.method.toLowerCase() == 'options')
//         res.send(200);
//     else
//         next();
// });

connection.connect();

let sql = "select * from test";
let insert = "insert into test(no,name,gender) values('3','lily','female')";
connection.query(sql, (err, rows, fields) => {
    if (err) throw err;

    app.get('/getTest', (req, res) => {
        res.send(rows);
    });
});

app.get('/insertTest', (req, res) => {
    console.log(req.query.id);
    // connection.query(insert, (err, rows, fields) => {
    //     if (err) throw err
    //     res.send(rows)
    // })
    res.send("-");
});

app.post('/insetPostData', (req, res, next) => {
    let form = new multiparty.Form();
    form.parse(req, (err, fields, file) => {
        console.log(fields.name[0]);
    });

    res.send(req.body);
});

app.get("/testGet", (req, res) => {
    let reqobj = querystring.parse(req.url.split("?")[1]);
    console.log(reqobj.name);
    console.log(reqobj);
    res.send(JSON.stringify("好吧"));
});

app.listen(port, () => {
    console.log(`http://localhost:${port}`);
});



connection.end();
